import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

//******************** 1.通讯配置部分 ********************
//1.1启用axios ----------
import axios from "axios";
Vue.prototype.axios = axios;
//阻止启动生产消息 console.log里面那一堆警告
Vue.config.productionTip = false;

//1.2启用websocket.io ----------
import VueSocketIO from "vue-socket.io";

//配置axios区别线上和测试环境 ----------
if (process.env.VUE_APP_TITLE === "test") {
  //依据环境变量-配置本地测试环境地址 注意这里要加//
  //axios baseURL地址
  axios.defaults.baseURL = "//localhost:3002";
  //websocket地址
  Vue.use(
    new VueSocketIO({
      debug: false,
      connection: "localhost:3004?sf=kf&name=admin"
    })
  );
} else {
  //依据环境变量-线上环境地址 注意这里要加//
  //线上环境是https的
  axios.defaults.baseURL = "//api.dangyunlong.com:3004";
  //配置线上webSocketIO地址
  Vue.use(
    new VueSocketIO({
      debug: false,
      connection: "api.dangyunlong.com:3004?sf=kf&name=admin"
    })
  );
}

//配置axios多请求拦截器 多个请求的时候一个一个发送并且调用element的loading框 ----------
import { Loading } from "element-ui"; //调用loading组件
import { Message } from "element-ui"; //调用Message组件
let loading;
//内存中正在请求的数量
let loadingNum = 0;
function startLoading() {
  if (loadingNum == 0) {
    loading = Loading.service({
      lock: true,
      text: "拼命加载中...",
      background: "rgba(255,255,255,0.5)"
    });
  }
  //请求数量加1
  loadingNum++;
}
function endLoading() {
  //请求数量减1
  loadingNum--;
  if (loadingNum <= 0) {
    loading.close();
  }
}
//请求数据拦截器
axios.interceptors.request.use(
  request => {
    startLoading();
    return request;
  },
  err => {
    return Promise.reject(err);
  }
);
//接收响应拦截器
axios.interceptors.response.use(
  response => {
    endLoading();
    return response;
  },
  err => {
    endLoading();
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = "请求错误(400)";
          break;
        case 401:
          this.$router.push("/login");
          break;
        case 403:
          err.message = "拒绝访问(403)";
          break;
        case 404:
          err.message = "请求出错(404)";
          break;
        case 408:
          err.message = "请求超时(408)";
          break;
        case 500:
          err.message = "服务器错误(500)";
          break;
        case 501:
          err.message = "服务未实现(501)";
          break;
        case 502:
          err.message = "网络错误(502)";
          break;
        case 503:
          err.message = "服务不可用(503)";
          break;
        case 504:
          err.message = "网络超时(504)";
          break;
        case 505:
          err.message = "HTTP版本不受支持(505)";
          break;
        default:
          err.message = `连接出错(${err.response.status})!`;
      }
    } else {
      err.message = "连接服务器失败!";
    }
    //通过Message发送错误警告
    Message.error(err.message);
    return Promise.reject(err);
  }
);
//1.3种加密方式 按需要加载
// import md5 from "js-md5";
// Vue.prototype.$md5 = md5;

// import sha256 from "js-sha256";
// Vue.prototype.$sha256 = sha256;

// import base64 from "js-base64";
// Vue.prototype.$base64 = base64;

//******************** 2.UI框架配置部分 ********************
//2.1启用element-ui框架 ----------
import ElementUI from "element-ui";
//通过cdn方式加载css
//import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI);

//2.2.启用swiper滚动图组件 ----------
// import Swiper from "swiper";

//2.3.启用jquery框架 ----------
import $ from "jquery";

//2.4启用vue-echarts组件 ----------
import echarts from "echarts";
Vue.prototype.$echarts = echarts;

//2.5 加载图片查看工具
import preview from "vue-photo-preview";
//通过cdn方式加载css
//import "vue-photo-preview/dist/skin.css";
var options = {
  fullscreenEl: false //关闭全屏按钮
};
Vue.use(preview, options);

//******************** 3.功能性插件配置部分 ********************
//3.1.启用MetaInfo动态标题 ----------
import MetaInfo from "vue-meta-info";
Vue.use(MetaInfo);

//3.2 启用图片懒加载 ----------
import VueLazyload from "vue-lazyload";
//初始化图片懒加载
Vue.use(VueLazyload, {
  preLoad: 1.3,
  //loading: ‘../static/loading.gif',
  loading: require("./assets/loading.gif"),
  attempt: 1
});

//3.3 启用图片压缩上传工具
import lrz from "lrz";

//******************** 4.媒体播放配置部分 ********************
//4.1video.js视频播放器
import videojs from "video.js";
//通过cdn方式加载css
//import 'video.js/dist/video-js.css'
Vue.prototype.$video = videojs;

//5.测试插件部分 ----------------------------------------
//5.1mock数据模拟 ----------------------------------------
//开启mock将会占用大约200kb的入口js空间 按需开启
//require("./mock/index");

//6.cookie ----------------------------------------
import Cookies from "js-cookie";
//设置cookie
//key/val/到期时间
// Cookies.set("name", "dangyunlong", { expires: 1 });
// //读取cookie
// console.log(Cookies.get("name"));
//删除cookie
Cookies.remove("name");

//7.逻辑部分 ----------------------------------------
//7.1 long.js x1
import long from "./js/longjs.js";
Vue.use(long);

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
